package com.hongdee.market.car.report.service.impl;

import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.market.bc.service.ITBcCustomItemService;
import com.hongdee.market.bc.service.ITBcCustomItemSocreService;
import com.hongdee.market.bc.service.ITBcCustomLevelConfService;
import com.hongdee.market.car.report.dao.ITmCarReportDao;
import com.hongdee.market.car.report.service.ITmCarReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hongdee.market.Constant.*;

/**
 * @author zhangkai
 * @desc 一句话描述
 **/
@Service
public class TmCarReportServiceImpl implements ITmCarReportService {
    @Autowired
    private ITmCarReportDao tmCarReportDao;
    /**
     * 注入车辆等级维护接口
     */
    @Autowired
    private ITBcCustomLevelConfService itBcCustomLevelConfService;
    /**
     * 注入评分项目维护接口
     */
    @Autowired
    private ITBcCustomItemService itBcCustomItemService;
    /**
     * 注入评分项目分值维护接口
     */
    @Autowired
    ITBcCustomItemSocreService itBcCustomItemSocreService;

    /***
     * 车辆等级-查询
     * @param start
     * @param limit
     * @param queryData
     * @param companyId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map page(String start, String limit, Map queryData, String companyId, String uid) throws Exception {
        /**当月***/
        System.out.println("测试");
        List params = new ArrayList(10);
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("  SELECT  c.id , ");
        sqlBuf.append("  u.u_name,");
        sqlBuf.append("  c.sa_id,");
        sqlBuf.append("  c.series,");
        sqlBuf.append("  c.displacement,");
        sqlBuf.append("  c.car_age_year,");
        sqlBuf.append("  c.vin,");
        sqlBuf.append("  c.plate_num,");
        sqlBuf.append("  c.owner_name,");
        sqlBuf.append("  c.owner_tel,");
        sqlBuf.append("  l.total_score,");
        sqlBuf.append("  l.car_level ");
        sqlBuf.append("  FROM");
        sqlBuf.append("  t_market_car_level l,");
        sqlBuf.append("  t_market_car c");
        sqlBuf.append("  LEFT JOIN t_bc_user u ON c.sa_id = u.id");
        sqlBuf.append("  WHERE");
        sqlBuf.append("  c.id = l.id");
        sqlBuf.append(" and c.company_id = ? ");
        params.add(companyId);

        // 不是系统管理员,查看自己
        if (!TokenUtil.isCompanyAdmin(uid)) {
            queryData.put("sa", uid);
        }
        createSqlAndParam(sqlBuf, params, queryData);
        sqlBuf.append(" order  by l.car_level asc ,l.total_score desc  ");
        return this.tmCarReportDao.page(sqlBuf.toString(), params.toArray(), start, limit);
    }

    /**
     * 车辆等级柱状态图
     *
     * @param queryData
     * @param companyId
     * @param uid
     * @return
     */
    @Override
    public List line(Map queryData, String companyId, String uid) throws Exception {
        List params = new ArrayList(10);
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("  SELECT ");
        sqlBuf.append("   round( ");
        sqlBuf.append("    ( ");
        sqlBuf.append("     count(IF(l.car_level = 'A', TRUE, NULL)) / count(l.id) ");
        sqlBuf.append("    ) * 100, ");
        sqlBuf.append("    2 ");
        sqlBuf.append("   ) AS A, ");
        sqlBuf.append("   round( ");
        sqlBuf.append("    ( ");
        sqlBuf.append("     count(IF(l.car_level = 'B', TRUE, NULL)) / count(l.id) ");
        sqlBuf.append("    ) * 100, ");
        sqlBuf.append("    2 ");
        sqlBuf.append("   ) AS B, ");
        sqlBuf.append("   round( ");
        sqlBuf.append("    ( ");
        sqlBuf.append("     count(IF(l.car_level = 'C', TRUE, NULL)) / count(l.id) ");
        sqlBuf.append("    ) * 100, ");
        sqlBuf.append("    2 ");
        sqlBuf.append("   ) AS C, ");
        sqlBuf.append("   round( ");
        sqlBuf.append("    ( ");
        sqlBuf.append("     count(IF(l.car_level = 'D', TRUE, NULL)) / count(l.id) ");
        sqlBuf.append("    ) * 100, ");
        sqlBuf.append("    2 ");
        sqlBuf.append("   ) AS D, ");
        sqlBuf.append("   round( ");
        sqlBuf.append("    ( ");
        sqlBuf.append("     count(IF(l.car_level = 'E', TRUE, NULL)) / count(l.id) ");
        sqlBuf.append("    ) * 100, ");
        sqlBuf.append("    2 ");
        sqlBuf.append("   ) AS E, ");
        sqlBuf.append("   round( ");
        sqlBuf.append("    ( ");
        sqlBuf.append("     count( ");
        sqlBuf.append("   ");
        sqlBuf.append("      IF (l.car_level IS NULL, TRUE, NULL) ");
        sqlBuf.append("     ) / count(l.id) ");
        sqlBuf.append("    ) * 100, ");
        sqlBuf.append("    2 ");
        sqlBuf.append("   ) AS '无' ");
        sqlBuf.append("  FROM");
        sqlBuf.append("  t_market_car_level l,");
        sqlBuf.append("  t_market_car c");
        sqlBuf.append("  LEFT JOIN t_bc_user u ON c.sa_id = u.id");
        sqlBuf.append("  WHERE");
        sqlBuf.append("  c.id = l.id");
        sqlBuf.append(" and c.company_id = ? ");
        params.add(companyId);


        // 不是系统管理员,查看自己
        if (!TokenUtil.isCompanyAdmin(uid)) {
            queryData.put("sa", uid);
        }
        createSqlAndParam(sqlBuf, params, queryData);
        List<Map<String, Object>> resMap = this.tmCarReportDao.list(sqlBuf.toString(), params.toArray());
        Map<String, Object> lineTempMap;
        if (resMap.size() > 0) {
            lineTempMap = resMap.get(0);
        } else {
            lineTempMap = new HashMap(6);
        }

        // 拼接 Line
        List resultList = new ArrayList(6);
        if (lineTempMap.containsKey(CAR_LEVEL_A)) {
            resultList.add(lineTempMap.get(CAR_LEVEL_A));
        } else {
            resultList.add(0);
        }
        if (lineTempMap.containsKey(CAR_LEVEL_B)) {
            resultList.add(lineTempMap.get(CAR_LEVEL_B));
        } else {
            resultList.add(0);
        }
        if (lineTempMap.containsKey(CAR_LEVEL_C)) {
            resultList.add(lineTempMap.get(CAR_LEVEL_C));
        } else {
            resultList.add(0);
        }
        if (lineTempMap.containsKey(CAR_LEVEL_D)) {
            resultList.add(lineTempMap.get(CAR_LEVEL_D));
        } else {
            resultList.add(0);
        }
        if (lineTempMap.containsKey(CAR_LEVEL_E)) {
            resultList.add(lineTempMap.get(CAR_LEVEL_B));
        } else {
            resultList.add(0);
        }
        if (lineTempMap.containsKey(CAR_LEVEL_NULL)) {
            resultList.add(lineTempMap.get(CAR_LEVEL_NULL));
        } else {
            resultList.add(0);
        }
        return resultList;
    }

    /***
     * 车辆等级饼状图
     * @param queryData
     * @param companyId
     * @param uid
     * @return
     */
    @Override
    public List pie(Map queryData, String companyId, String uid) throws Exception {
        List params = new ArrayList(10);
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("  SELECT    ");
        sqlBuf.append("  if(l.car_level is null,'无',l.car_level) as name ,count(l.id) as value ");
        sqlBuf.append("  FROM");
        sqlBuf.append("  t_market_car_level l,");
        sqlBuf.append("  t_market_car c");
        sqlBuf.append("  LEFT JOIN t_bc_user u ON c.sa_id = u.id");
        sqlBuf.append("  WHERE");
        sqlBuf.append("  c.id = l.id");
        sqlBuf.append(" and c.company_id = ? ");
        params.add(companyId);

        // 不是系统管理员,查看自己
        if (!TokenUtil.isCompanyAdmin(uid)) {
            queryData.put("sa", uid);
        }
        createSqlAndParam(sqlBuf, params, queryData);
        sqlBuf.append(" group by l.car_level ");
        return this.tmCarReportDao.list(sqlBuf.toString(), params.toArray());
    }

    /**
     * 根据当前目标计算可提升的建议
     *
     * @param vin
     * @param companyId
     * @param uid
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List advise(String vin, String companyId, String uid, String id) throws Exception {
        /***
         * 可提升项目：来店保养
         *            购买续保
         *            购买延保
         *            储值
         *            会员
         * */
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT     ");
        sql.append("   c.id,     ");
        sql.append("   c.vin,     ");
        sql.append("   c.plate_num,     ");
        sql.append("   c.owner_name,     ");
        sql.append("   c.owner_tel,     ");
        sql.append("   c.car_age_year,     ");
        sql.append("   c.pre_service_date,     ");
        sql.append("   c.guarantee_start,     ");
        sql.append("   c.run_mile,     ");
        sql.append("   l.run_mile_day,     ");
        sql.append("   l.maintain_avg_mile,     ");
        sql.append("   c.member_level, l.member_score  ,  ");
        sql.append("   c.pre_pay_type,l.pre_pay_score  ,   ");
        sql.append("   l.car_level,     ");
        sql.append("   l.total_score,     ");
        sql.append("   l.is_exsurance,  l.is_exsurance_score,   ");
        sql.append("   l.is_insurance_self ,l.is_insurance_self_score    ");
        sql.append("      ");
        sql.append(" FROM     ");
        sql.append("   t_market_car c,     ");
        sql.append("   t_market_car_level l     ");
        sql.append(" WHERE     ");
        sql.append("   c.id = l.id AND c.id = ? ");
        List params = new ArrayList();
        params.add(id);

        Map map = this.tmCarReportDao.info(sql.toString(), params.toArray());
        if (map == null || map.size() == 0) {
            throw new Exception("未查询到有效数据");
        }
        vin = String.valueOf(map.get("vin"));
        //定义结果集
        List resultMap = new ArrayList();
        // 当前等级，当前得分，目标等级，目标分值
        Map carInfo = new HashMap(7);
        String curLevel = String.valueOf(map.get("carLevel"));
        curLevel = ("null".equals(curLevel) || "".equals(curLevel)) ? "无" : curLevel;
        carInfo.put("curLevel", "车辆等级：" + curLevel);
        carInfo.put("curScore", map.get("totalScore"));
        //获取目标等级
        switch (String.valueOf(carInfo.get("curLevel"))) {
            case CAR_LEVEL_A:
                carInfo.put("targetLevel", CAR_LEVEL_A);
                carInfo.put("advise", "无需升级");
                break;
            case CAR_LEVEL_B:
                carInfo.put("targetLevel", CAR_LEVEL_A);
                carInfo.put("advise", "可提升到" + CAR_LEVEL_A + "级");
                break;
            case CAR_LEVEL_C:
                carInfo.put("targetLevel", CAR_LEVEL_B);
                carInfo.put("advise", "可提升到" + CAR_LEVEL_B + "级");
                break;
            case CAR_LEVEL_D:
                carInfo.put("targetLevel", CAR_LEVEL_C);
                carInfo.put("advise", "可提升到" + CAR_LEVEL_C + "级");
                break;
            case CAR_LEVEL_E:
                carInfo.put("targetLevel", CAR_LEVEL_D);
                carInfo.put("advise", "新车无法升级，但可提升分数");
                break;
            default:
                String newCarFlag = "1";
                String carAgeYear = String.valueOf(map.get("carAgeYear"));
                if (!newCarFlag.equals(carAgeYear)) {
                    carInfo.put("targetLevel", CAR_LEVEL_D);
                    carInfo.put("advise", "车龄不等于1，可升到D级");
                } else {
                    carInfo.put("targetLevel", CAR_LEVEL_E);
                    carInfo.put("advise", "车龄为1，可提升到为E级");
                }
                break;
        }

        Map carLevelConf = itBcCustomLevelConfService.code(carInfo.get("targetLevel").toString(), companyId);
        if (carLevelConf == null || carLevelConf.size() == 0) {
            carInfo.put("targetScore", "系统未设置目标");
        } else {
            String type = String.valueOf(carLevelConf.get("scoreType"));
            if (type.equals(SCORE_TYPE_SCORE)) {
                carInfo.put("targetScore", "分值：" + carLevelConf.get("start"));
            } else {
                Map converItem = itBcCustomItemService.convert();
                String itemName = "[无效项目]";
                if (converItem != null) {
                    itemName = String.valueOf(converItem.get(String.valueOf(carLevelConf.get("scoreItem"))));
                }
                carInfo.put("targetScore", itemName + "值为" + carLevelConf.get("start"));
            }
        }
        carInfo.put("targetLevel", carInfo.get("targetLevel"));
        resultMap.add(carInfo);

        /***会员等级***/
        Map memberInfo = new HashMap(4);
        String level = String.valueOf(map.get("memberLevel"));
        level = ("null".equals(level) || "".equals(level)) ? "无" : level;
        Map memberCovMap = itBcCustomItemSocreService.scoreConver(companyId, MEMBER_LEVEL);
        switch (level) {
            case "金卡":
                memberInfo.put("targetLevel", "金卡");
                memberInfo.put("targetScore", "" + memberCovMap.get(3));
                memberInfo.put("advise", "无需升级");
                break;
            case "银卡":
                memberInfo.put("targetLevel", "金卡");
                memberInfo.put("targetScore", "" + memberCovMap.get(3));
                memberInfo.put("advise", "可升级金卡");
                break;
            case "普卡":
                memberInfo.put("targetLevel", "金卡；银卡");
                memberInfo.put("targetScore", "" + memberCovMap.get(3) + ";" + memberCovMap.get(2));
                memberInfo.put("advise", "可升级金卡；银卡");
                break;
            default:
                memberInfo.put("targetLevel", "金卡；银卡；普卡");
                memberInfo.put("targetScore", "" + memberCovMap.get(3) + ";" + memberCovMap.get(2) + ";" + memberCovMap.get(1));
                memberInfo.put("advise", "可升级金卡；银卡；普卡");
                break;
        }
        //当前会员等级与分值
        memberInfo.put("curLevel", "当前会员:" + level);
        memberInfo.put("curScore", map.get("memberScore"));
        //会员等级
        resultMap.add(memberInfo);

        /***是否储值***/
        Map preMap = new HashMap(5);
        String prePayType = String.valueOf(map.get("prePayType"));

        prePayType = ("null".equals(prePayType) || "".equals(prePayType)) ? "无" : prePayType;
        Map preCovMap = itBcCustomItemSocreService.scoreConver(companyId, PRE_PAY_TYPE);
        switch (prePayType) {
            case "俱乐部":
                preMap.put("targetLevel", "俱乐部");
                preMap.put("targetScore", preCovMap.get(2));
                preMap.put("advise", "无需升级");
                break;
            case "自店":
                preMap.put("targetLevel", "俱乐部");
                preMap.put("targetScore", preCovMap.get(2));
                preMap.put("advise", "可升级俱乐部");
                break;
            default:
                preMap.put("targetLevel", "俱乐部；自店");
                preMap.put("targetScore", preCovMap.get(2) + ";" + preCovMap.get(1));
                preMap.put("advise", "可升级俱乐部;自店");
                prePayType = "无";
                break;
        }
        //是否储值
        preMap.put("curLevel", "当前储值：" + prePayType);
        preMap.put("curScore", map.get("prePayScore"));
        //是否储值
        resultMap.add(preMap);


        /***是否延保***/
        Map exsuranceMap = new HashMap(5);
        Map exSuanceCovMap = itBcCustomItemSocreService.scoreConver(companyId, IS_EXTENDED);
        String exsurance = String.valueOf(map.get("isExsurance"));

        exsuranceMap.put("targetLevel", "购买延保");
        exsuranceMap.put("targetScore", exSuanceCovMap.get(1));
        switch (exsurance) {
            case "1":
                exsuranceMap.put("advise", "已购买延保无需升级");
                exsurance = "已购买";
                break;
            default:
                exsurance = "未购买";
                int carAge = 0;
                try {
                    carAge = Integer.parseInt(String.valueOf(map.get("carAgeYear")));
                } catch (Exception e) {
                    exsuranceMap.put("advise", "无有效车龄，可询问？");
                    break;
                }
                if (carAge <= 3) {
                    exsuranceMap.put("advise", "3年内车可购买延保");
                    break;
                } else {
                    exsuranceMap.put("advise", "车龄已超3年，无法购买延保");
                }

                break;
        }
        exsuranceMap.put("curLevel", "延保：" + exsurance);
        exsuranceMap.put("curScore", map.get("isExsuranceScore"));
        resultMap.add(exsuranceMap);


        /***是否续保***/
        String insurance = String.valueOf(map.get("isInsuranceSelf"));
        Map insuranceMap = new HashMap(5);
        Map insuranceCovMap = itBcCustomItemSocreService.scoreConver(companyId, IS_EXTENDED);
        insuranceMap.put("targetLevel", "购买续保");
        insuranceMap.put("targetScore", insuranceCovMap.get(1));
        switch (insurance) {
            case "1":
                insuranceMap.put("advise", "存在当期保险，无需升级");
                insurance = "已购买";
                break;
            default:
                insurance = "未购买";
                //无续保且当前车辆的上牌日期落在附近，质保开始时间
                String startDate = map.get("guaranteeStart").toString();
                //保险提前3个月出单,
                try {
                    // 当前日期晚于 在出保日期前90天 且 早于出保日期后7天 可提醒
                    String cbDate = DateUtils.getTimeStr("yyyy") + "-" + startDate.substring(6);
                    long cbTime = DateUtils.parseTime(cbDate, "yyyy-MM-dd");
                    long preTime = DateUtils.addDayLongTime(cbTime, -90);
                    long now = DateUtils.getNowTime();
                    if (preTime <= now && now <= DateUtils.addDayLongTime(cbTime, 7)) {
                        insuranceMap.put("advise", "保险到期日" + cbDate + ",可购买");
                    } else {
                        if (cbTime < now) {
                            cbDate = DateUtils.formatLongTime(DateUtils.addMonthLongTime(cbTime, 12), "yyyy-MM-dd");
                        }
                        insuranceMap.put("advise", "保险到期日" + cbDate + ",无法购买");
                    }
                } catch (Exception e) {
                    insuranceMap.put("advise", "无有效出单日期,无法预计");
                }

                break;
        }
        insuranceMap.put("curLevel", "续保：" + insurance);
        insuranceMap.put("curlScore", map.get("isInsuranceSelfScore"));

        // 续保任务
        // 保养任务
        if (hasTask(companyId, vin, "bx")) {
            insuranceMap.put("task", "任务");
        } else {
            insuranceMap.put("task", "");
        }
        resultMap.add(insuranceMap);

        /****
         * 保养提醒
         */
        Map byMap = new HashMap(5);
        String bxDate = String.valueOf(map.get("preServiceDate"));
        bxDate = "null".equals(bxDate) ? "" : bxDate;
        if (!"".equals(bxDate)) {
            String preDate = " ";
            //保养间隔里程默认为 5000
            String avgMile = map.get("maintainAvgMile") == null ? MAINTAIN_AVG_MILE : map.get("maintainAvgMile").toString();
            //日平均行驶里程
            String runMileDay = map.get("runMileDay") == null ? ZERO_STRING : map.get("runMileDay").toString();
            String bxResult = "按时间";
            //保养间隔天数
            int days = MAINTAIN_AVG_DAYS;
            try {
                BigDecimal runMileD = new BigDecimal(runMileDay);
                BigDecimal avgMileD = new BigDecimal(avgMile);
                long nowTime = DateUtils.getNowTime();
                long preByTime = DateUtils.parseTime(bxDate, "yyyy-MM-dd");
                if (!runMileD.equals(new BigDecimal(ZERO_STRING)) && (avgMileD.divide(runMileD, RoundingMode.HALF_UP).intValue() < MAINTAIN_AVG_DAYS)) {
                    days = avgMileD.divide(runMileD, RoundingMode.HALF_UP).intValue();
                    if (DateUtils.addDayLongTime(preByTime, days) > nowTime) {
                        bxResult = "按里程";
                    }
                }
                long suggestByTIme = DateUtils.addDayLongTime(preByTime, days);

                preDate = DateUtils.formatLongTime(suggestByTIme, "yyyy-MM-dd");
                // 提前15天提醒
                if (suggestByTIme < DateUtils.addDayLongTime(nowTime, MAINTAIN_PRE_DAYS)) {
                    byMap.put("advise", bxResult + "于" + preDate + "到保养节点，可提醒");
                } else {
                    byMap.put("advise", bxResult + "未到保养节点，无需提醒");
                }
            } catch (Exception e) {
                byMap.put("advise", "计算出错，无建议");
            }

            byMap.put("curLevel", "保养" + bxDate);
            byMap.put("curScore", "");
            byMap.put("targetLevel", "预计" + preDate);
            byMap.put("targetScore", "");

        } else {
            byMap.put("curLevel", "保养:无日期");
            byMap.put("curScore", "");
            byMap.put("targetLevel", "预计:无日期");
            byMap.put("targetScore", "");
            byMap.put("advise","无法预计，建议查看 DMS");

        }
        // 保养任务
        if (hasTask(companyId, vin, "by")) {
            byMap.put("task", "任务");
        } else {
            byMap.put("task", "");
        }
        resultMap.add(byMap);
        return resultMap;
    }


    /**
     * 拼接 SQL
     *
     * @param sqlBuf
     * @param params
     * @param queryData
     * @throws Exception
     */
    private void createSqlAndParam(StringBuffer sqlBuf, List params, Map queryData) throws Exception {


        //sa不为空
        if (queryData.containsKey("sa") && !StringUtils.isEmpty(queryData.get("sa"))) {
            sqlBuf.append(" and c.sa_id = ? ");
            params.add(String.valueOf(queryData.get("sa")));
        }

        if (queryData.containsKey("vin") && !StringUtils.isEmpty(queryData.get("vin"))) {
            sqlBuf.append(" and c.vin like ? ");
            params.add("%" + String.valueOf(queryData.get("vin")) + "%");
        }

        if (queryData.containsKey("plateNum") && !StringUtils.isEmpty(queryData.get("plateNum"))) {
            sqlBuf.append(" and c.plate_num like ? ");
            params.add("%" + String.valueOf(queryData.get("plateNum")) + "%");
        }

        if (queryData.containsKey("series") && !StringUtils.isEmpty(queryData.get("series"))) {
            sqlBuf.append(" and locate(?,c.series) ");
            params.add(String.valueOf(queryData.get("series")));
        }
        if (queryData.containsKey("pl") && !StringUtils.isEmpty(queryData.get("pl"))) {
            sqlBuf.append(" and c.displacement = ?  ");
            params.add(String.valueOf(queryData.get("pl")));
        }

        if (queryData.containsKey("level") && !StringUtils.isEmpty(queryData.get("level"))) {
            if ("无".equals(String.valueOf(queryData.get("level")))) {
                sqlBuf.append(" and l.car_level is  null ");

            } else {
                sqlBuf.append(" and l.car_level = ?  ");
                params.add(String.valueOf(queryData.get("level")));
            }

        }

        // 车龄
        if (queryData.containsKey("age") && !StringUtils.isEmpty(queryData.get("age"))) {
            int age = Integer.parseInt(String.valueOf(queryData.get("age")));
            if (age > 8) {
                sqlBuf.append(" and c.car_age_year >= ?  ");
                params.add(age);
            } else {
                sqlBuf.append(" and l.car_age_year = ?  ");
                params.add(age);
            }

        }
        // 车龄
        if (queryData.containsKey("advise") && !StringUtils.isEmpty(queryData.get("advise"))) {
            String advise = String.valueOf(queryData.get("advise"));

            switch (advise) {
                case "续保":
                    sqlBuf.append(" and (l.is_insurance_self !='是' or l.is_insurance_self is null )   ");

                    break;
                case "延保":
                    sqlBuf.append(" and (l.is_exsurance !='是' or l.is_exsurance is null )   ");
                    break;
                case "会员":
                    sqlBuf.append(" and (l.member_level = '' or l.member_level is null )  ");
                    break;
                case "储值":
                    sqlBuf.append(" and and (l.pre_pay_type = '' or l.pre_pay_type is null )   ");
                    break;
                default:
                    break;
            }
        }
    }

    /***
     * 判断该车辆是否存在任务
     * @param companyId
     * @param vin
     * @param type
     * @return
     * @throws Exception
     */
    private boolean hasTask(String companyId, String vin, String type) throws Exception {
        String sql = "select 1 from t_crm_task_real where company_id = ? and type_pcode=? and vin = ? and status = 0 ";
        List params = new ArrayList();
        params.add(companyId);
        params.add(type);
        params.add(vin);
        List ls = this.tmCarReportDao.list(sql, params.toArray());
        return (ls != null && ls.size() > 0);

    }
}
